package com.fudan.framework.utils;

import net.sourceforge.pinyin4j.PinyinHelper;

public class PinyinUtils {
	public static String toPinyin(String str) {
		char[] chars = str.toCharArray();
		StringBuilder full = new StringBuilder();
		for (Character c : chars) {
			if((c<48)||((c>57)&(c<65))||(c>122)||((c>90)&(c<97))) {
				String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(c);
				if(pinyin != null && pinyin.length>0) {
					full.append(pinyin[0].replaceAll("\\d", ""));
				}
			}else {
				full.append(c);
			}

		}
		return full.toString();
	}

	public static String toPinyinAbbr(String str) {
		char[] chars = str.toCharArray();
		StringBuilder abbr = new StringBuilder();
		for (char c : chars) {
			if((c<48)||((c>57)&(c<65))||(c>122)||((c>90)&(c<97))) {
				String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(c);
				if(pinyin != null && pinyin.length>0) {
					abbr.append(pinyin[0].charAt(0));
				}
			}else {
				abbr.append(c);
			}

		}
		return abbr.toString();
	}
}
